{extend name="table"}

{block name="button"}
{if isset($type) and $type eq 'index'}
<!--{if auth("add")}-->
<button data-open='{:url("add")}' class='layui-btn layui-btn-sm layui-btn-primary'>{:lang('添加海报')}</button>
<!--{/if}-->
<!--{if auth("state")}-->
<a data-confirm="{:lang('确定要批量禁用海报吗？')}" data-table-id="PosterTable" data-action="{:url('state')}" data-rule="id#{id};status#0" class='layui-btn layui-btn-sm layui-btn-primary'>{:lang('批量禁用')}</a>
<!--{/if}-->
{else}
<!--{if auth("state")}-->
<a data-confirm="{:lang('确定要批量恢复海报吗？')}" data-table-id="PosterTable" data-action="{:url('state')}" data-rule="id#{id};status#1" class='layui-btn layui-btn-sm layui-btn-primary'>{:lang('批量恢复')}</a>
<!--{/if}-->
<!--{if auth("remove")}-->
<a data-confirm="{:lang('确定永久删除海报吗？')}" data-table-id="PosterTable" data-action='{:url("remove")}' data-rule="id#{id}" class='layui-btn layui-btn-sm layui-btn-primary'>{:lang('批量删除')}</a>
<!--{/if}-->
{/if}
{/block}

{block name="content"}
<div class="layui-tab layui-tab-card">
    <ul class="layui-tab-title">
        {foreach ['index'=>lang('推广海报'),'recycle'=>lang('回 收 站')] as $k=>$v}{if isset($type) and $type eq $k}
        <li data-open="{:url('index')}?type={$k}" class="layui-this color-green">{$v}</li>
        {else}
        <li data-open="{:url('index')}?type={$k}">{$v}</li>
        {/if}{/foreach}
    </ul>
    <div class="layui-tab-content">
        {include file='base/poster/index_search'}
        <table id="PosterTable" data-url="{:request()->url()}"></table>
    </div>
</div>
{/block}

{block name='script'}
<script>
    $(function () {
        // 初始化表格组件
        let $table = $('#PosterTable').layTable({
            even: true, height: 'full',
            sort: {field: 'sort desc,id', type: 'desc'},
            cellExpandedMode: 'tips',
            cols: [[
                {checkbox: true, fixed: true},
                {field: 'sort', title: '{:lang("排序权重")}', width: 100, align: 'center', sort: true, templet: '#SortInputTpl'},
                {field: 'code', title: '{:lang("海报编号")}', align: 'center', width: 160, sort: true},
                {field: 'name', title: '{:lang("海报名称")}', align: 'center', minWidth: 130},
                {
                    field: 'levels', title: '{:lang("授权等级")}', templet: function (d) {
                        let tpls = [];
                        (d.levels_names || []).map(function (v) {
                            tpls.push('<span class="layui-badge layui-bg-blue">' + v + '</span>')
                        });
                        return tpls.join('');
                    }
                },
                {
                    field: 'devices', title: '{:lang("授权终端")}', templet: function (d) {
                        let tpls = [];
                        (d.devices_names || []).map(function (v) {
                            tpls.push('<span class="layui-badge layui-bg-blue">' + v + '</span>')
                        });
                        return tpls.join('');
                    }
                },
                {field: 'status', title: '{:lang("账号状态")}', align: 'center', minWidth: 110, templet: '#StatusSwitchTpl'},
                {field: 'create_time', title: '{:lang("创建时间")}', align: 'center', minWidth: 170},
                {toolbar: '#toolbar', title: '{:lang("操作面板")}', align: 'center', minWidth: 210, fixed: 'right'},
            ]]
        });

        // 数据状态切换操作
        layui.form.on('switch(StatusSwitch)', function (obj) {
            let data = {id: obj.value, status: obj.elem.checked > 0 ? 1 : 0};
            $.form.load("{:url('state')}", data, 'post', function (ret) {
                let fn = () => $table.trigger('reload');
                ret.code > 0 ? fn() : $.msg.error(ret.info, 3, fn)
                return false;
            }, false);
        });
    });
</script>

<!-- 列表排序权重模板 -->
<script type="text/html" id="SortInputTpl">
    <input type="number" min="0" data-blur-number="0" data-action-blur="{:sysuri()}" data-value="id#{{d.id}};action#sort;sort#{value}" data-loading="false" value="{{d.sort}}" class="layui-input text-center">
</script>

<!-- 数据状态切换模板 -->
<script type="text/html" id="StatusSwitchTpl">
    <!--{if auth("state")}-->
    <input type="checkbox" value="{{d.id}}" lay-skin="switch" lay-text="{:lang('已激活')}|{:lang('已禁用')}" lay-filter="StatusSwitch" {{d.status>0?'checked':''}}>
    <!--{else}-->
    {{d.status ? '<b class="color-green">' + '{:lang("已启用")}' + '</b>' : '<b class="color-red">' + '{:lang("已禁用")}' + '</b>'}}
    <!--{/if}-->
</script>

<!-- 数据操作工具条模板 -->
<script type="text/html" id="toolbar">
    {if isset($type) and $type eq 'index'}
    <!-- {if auth("edit")} -->
    <a class="layui-btn layui-btn-sm" data-open='{:url("edit")}?id={{d.id}}'>{:lang('编 辑')}</a>
    <!--{/if}-->
    {else}
    <!--{if auth("edit")} -->
    <a class="layui-btn layui-btn-sm" data-open='{:url("edit")}?id={{d.id}}'>{:lang('编 辑')}</a>
    <!--{/if}-->
    <!--{if auth("remove")}-->
    <a class="layui-btn layui-btn-sm layui-btn-danger" data-confirm="{:lang('确定要永久删除此海报吗？')}" data-action="{:url('remove')}" data-value="id#{{d.id}}">{:lang('删 除')}</a>
    <!--{/if}-->
    {/if}
</script>
{/block}